package main

import (
	"cmp"
	"fmt"
	"sort"
)

func main() {
	//sort1()

	//sort2_asc()

	sort3_desc()
}

func sort1() {
	data := []int{1, 8, 3, 5, 9, 2, 4, 6, 7}
	fmt.Println(data)
	sort.Ints(data)
	fmt.Println(data)
	v := 8
	i := sort.SearchInts(data, v)
	if i < len(data) {
		if data[i] != v {
			data = append(data, 0)
			for j := len(data) - 1; j > i; j-- {
				data[j] = data[j-1]
			}
			data[i] = v
		}
	} else {
		data = append(data, v)
	}
	fmt.Println(data)
	fmt.Println(data[i] == v)
}

func sort2_asc() {
	data := []int{1, 8, 3, 5, 9, 2, 6, 7}
	fmt.Println(data)
	sort.Ints(data) //asc
	fmt.Println(data)

	v := 10
	i, found := sort.Find(len(data), func(i int) int {
		return cmp.Compare(v, data[i]) //asc 必须这样写
	})
	if found {
		fmt.Println(i, found)
	} else {
		if i < len(data) {
			data = append(data, 0)
			for j := len(data) - 1; j > i; j-- {
				data[j] = data[j-1]
			}
			data[i] = v
		} else {
			data = append(data, v)
		}
		fmt.Println(i, data)
	}
}

func sort3_desc() {
	data := []int{1, 8, 3, 5, 9, 2, 6, 7}
	fmt.Println(data)
	sort.Sort(sort.Reverse(sort.IntSlice(data))) //desc
	fmt.Println(data)

	v := 4
	i, found := sort.Find(len(data), func(i int) int {
		return cmp.Compare(data[i], v) //desc 必须这样写
	})
	if found {
		fmt.Println(i, found)
	} else {
		if i < len(data) {
			data = append(data, 0)
			for j := len(data) - 1; j > i; j-- {
				data[j] = data[j-1]
			}
			data[i] = v
		} else {
			data = append(data, v)
		}
		fmt.Println(i, data)
	}
}
